import './public-path.js'
import { createApp } from 'vue'
import App from './App.vue'
import routes from './router'
import { createRouter, createWebHistory } from 'vue-router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import btnPermi from '@/directive/btnPermi.js'
import hsPackages from 'hs-packages'
import 'hs-packages/style.css'

// 引入echarts
// import echarts from '@/utils/echarts.js'


// 挂载到vue实例中

// let app = createSSRApp(App)

// Vue.prototype.$echarts = echarts;//vue2的挂载方式
// app.config.globalProperties.$echarts = echarts;//vue3的挂载方式


// 引入 echarts
import * as echarts from "echarts";
const app = createApp(App);
// 全局挂载 echarts
app.config.globalProperties.$echarts = echarts

// 汉化 element plus
import locale from "element-plus/es/locale/lang/zh-cn";
import actions from '@/utils/qiankunAction'
import log from './directive/log.js'
import { reportLog } from '@/utils/reportLog'
import '@/assets/fontIcon/iconfont.css'
// createApp(App).use(store).use(router).mount('#qiankun-child')

let router = null;
let instance = null;
let history = null;

function render(props = {}) {
    if (props) {
        actions.setActions(props)
    }
    const { container } = props;
    // history = createWebHistory(window.__POWERED_BY_QIANKUN__ ? '/qiankun-vue3-child' : '/');
    history = createWebHistory(process.env.BASE_URL);
    router = createRouter({
        history,
        // routes: routes.options.routes,
        routes: routes,
    });
    router.beforeEach((to, from, next) => {
        // 每次进入页面要增加日志
        reportLog({ type: 3, pageKey: to.query.rid ? to.query.rid : to.path }) // 进入页面上报日志
        next()
    })
    instance = createApp(App);
    for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
        instance.component(key, component)
    }
    instance.use(ElementPlus, { locale });
    instance.use(hsPackages);
    instance.use(router);
    instance.use(store);
    instance.directive('btnPermi', btnPermi)
    instance.directive('log', log)
    instance.mount(container ? container.querySelector('#qiankun-dashboard-pc') : '#qiankun-dashboard-pc');
}
if (!window.__POWERED_BY_QIANKUN__) {
    render();
}

export async function bootstrap() {
    console.log('%c%s', 'color: green;', 'vue3.0 app bootstraped');
}
function storeTest(props) {
    props.onGlobalStateChange &&
        props.onGlobalStateChange(
            (value, prev) => console.log(`[onGlobalStateChange - ${props.name}]:`, value, prev),
            true,
        );
    props.setGlobalState &&
        props.setGlobalState({
            ignore: props.name,
            user: {
                name: props.name,
            },
        });
}
export async function mount(props) {
    storeTest(props);
    render(props);
    instance.config.globalProperties.$onGlobalStateChange = props.onGlobalStateChange;
    instance.config.globalProperties.$setGlobalState = props.setGlobalState;
}

export async function unmount() {
    instance.unmount();
    instance._container.innerHTML = '';
    instance = null;
    router = null;
    history.destroy();
}
export async function update(props) {
    // 更新
    render(props);
}
